package com.glut.forum.controller;

import com.glut.forum.bean.dto.UserDTO;
import com.glut.forum.bean.entity.User;
import com.glut.forum.bean.vo.UserVO;
import com.glut.forum.common.annotations.CurrentUserInfo;
import com.glut.forum.common.beans.ResultBean;
import com.glut.forum.common.beans.UserInfo;
import com.glut.forum.service.IUserService;
import com.glut.forum.service.QnUploadService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author miloYe
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/forum/user")
@Slf4j
@AllArgsConstructor
public class UserController {

    private final IUserService userService;
    private final QnUploadService qnUploadService;
    private final MapperFacade mapperFacade;

    /**
     * 获取当前登录用户的详细信息
     */
    @GetMapping("/account")
    public ResultBean<UserDTO> getAccountDetail(@CurrentUserInfo UserInfo userInfo) {
        return new ResultBean<>(userService.getUserFormatToUserDTO(userInfo));
    }

    /**
     * 根据id获取用户信息(详细信息)
     */
    @GetMapping("/info/detail")
    public ResultBean<UserDTO> getUserDetailByID(Integer id) {
        return new ResultBean<>(userService.getUserFormatToUserDTO(id));
    }

    /**
     * 根据id获取用户信息(不是详细信息)
     */
    @GetMapping("/info")
    public ResultBean<UserVO> getUserInfoByID(Integer id) {
        return new ResultBean<>(userService.getUserInfoById(id));
    }

    /**
     * 保存用户(单个)
     *
     * @param user
     * @return UserVO
     */
    @PostMapping("/save")
    public ResultBean<UserVO> savaUser(User user) {
        return new ResultBean<>(userService.saveUser(user));
    }

    /**
     *
     * 修改当前登录用户信息
     *
     * @param uv
     * @return
     */
    @PostMapping("/update")
    public ResultBean<UserVO> updateLoginUser(UserVO uv, @CurrentUserInfo UserInfo userInfo) {
        return new ResultBean<>(userService.updateLoginUserInfo(uv, userInfo));
    }

    /**
     * 用户登录接口
     *
     * 登录，如果登录成功返回三个参数 token token名字 过期时间 和 用户基本信息
     *
     * Map.put("access_token", token);
     * Map.put("token_type", "token");
     * Map.put("expires_in", "14");
     * Map.put("user", mapperFacade.map(u, UserVO.class));
     *
     * token里面载荷存放了userid
     *
     * 因为map里面参数较少，就不创建VO对象，直接使用Map回传
     *
     */
    @PostMapping("/login")
    public ResultBean<Map<String, Object>> login(String account, String password) {
        return new ResultBean<>(userService.login(account, password));
    }

    /**
     * 退出登录
     *
     * @param userInfo
     * @return 是否退出成功
     */
    @GetMapping("/logout")
    public ResultBean<Boolean> logout(@CurrentUserInfo UserInfo userInfo) {
        return new ResultBean<>(userInfo != null);
    }

    /**
     * 查询邮箱是否存在
     * @param email
     * @return
     */
    @PostMapping("/status/email")
    public ResultBean<Boolean> existEmail(String email) {
        return new ResultBean<>(userService.checkStatus(email, "email"));
    }

    /**
     * 查询电话号码是否存在
     * @param phone
     * @return
     */
    @PostMapping("/status/phone")
    public ResultBean<Boolean> existPhone(String phone) {
        return new ResultBean<>(userService.checkStatus(phone, "phone"));
    }

    /**
     * 修改密码
     *
     * @param password
     * @param userInfo
     * @return
     */
    @PostMapping("/pwd/update")
    public ResultBean<Boolean> updatePwd(String password, @CurrentUserInfo UserInfo userInfo) {
        return new ResultBean<>(userService.updatePwd(password, userInfo));
    }

    /**
     * 验证当前用户的密码
     *
     * @param password
     * @param userInfo
     * @return
     */
    @PostMapping("pwd/auth")
    public ResultBean<Boolean> authPwd(String password, @CurrentUserInfo UserInfo userInfo) {
        return new ResultBean<>(userService.authPwd(password, userInfo));
    }

    /**
     * 忘记密码, 应该先验证邮箱是否存在,然后验证邮箱身份,然后通过邮箱修改密码
     *
     * @param email
     * @return
     */
    @PostMapping("/pwd/forget")
    public ResultBean<Boolean> emailCheck(String email) {
        return new ResultBean<>(userService.emailCheck(email));
    }

    /**
     *
     * 通过邮箱修改密码
     *
     * @param password
     * @param email
     * @return
     */
    @PostMapping("/pwd/update/email")
    public ResultBean<Boolean> updatePwdByEmail(String password, String email) {
        return new ResultBean<>(userService.updatePwdByEmail(password, email, null));
    }

    /**
     *
     * 修改用户头像
     *
     */
    @PostMapping("/avatar/update")
    public ResultBean<Boolean> uploadAvatar(@RequestParam("file") MultipartFile file, @CurrentUserInfo UserInfo userInfo) {
        return new ResultBean<>(userService.uploadAvatar(file, userInfo));
    }
}
